#include <stdio.h>

#define MaxSize 10

typedef int ElemType_Tree;

typedef struct {
    ElemType_Tree data[MaxSize];
    int top;
}SqStack;

// 初始化一个顺序存储的栈
void InitSqStack(SqStack &S) {
    S.top = -1;
}

// 入栈
bool PushElem(SqStack &S, ElemType_Tree e) {
    // 判断栈满则无法入栈
    if (MaxSize -1 == S.top) {
        return false;
    }
    // 元素入栈
    S.data[++S.top] = e;
    return true;
}

// 出栈，并返回弹出的元素
bool PopElem(SqStack &S, ElemType_Tree &e) {
    // 判断栈空则无法出栈
    if (-1 == S.top) {
        return false;
    }
    // 元素出栈
    e = S.data[S.top--];
    return true;
}

// 打印栈中元素
void PrintSqStack(SqStack S) {
    printf("底 ");
    for (int i = 0; i <= S.top; ++i) {
        printf("|%d", S.data[i]);
    }
    printf("|_ 顶\n");
}

int main() {
    printf("Hello, SqStack!\n");

    // 初始化一个顺序栈
    SqStack S;
    InitSqStack(S);
    PrintSqStack(S);

    // 入栈
    ElemType_Tree e_push;
    printf("Maxsize of the stack is %d", MaxSize);
    printf("Enter the value of element to push (with '9999' to end): ");
    scanf("%d", &e_push);
    while(9999 != e_push) {
        if (PushElem(S, e_push)) {
            printf("Success to push!\n");
            PrintSqStack(S);
        } else {
            printf("Fail to push..\n");
            break;
        }
        printf("Enter the element: ");
        scanf("%d", &e_push);
    }
    PrintSqStack(S);


    return 0;
}
